package org.seven.jrdp.web.mgt.sys;

import java.util.List;
import java.util.Map;

import javax.servlet.ServletContext;
import javax.servlet.http.HttpServletRequest;

import org.seven.jrdp.biz.sys.model.SysParam;
import org.seven.jrdp.biz.sys.service.SysParamService;
import org.seven.jrdp.commons.config.ParamConfig;
import org.seven.jrdp.commons.constant.GlobalContants;
import org.seven.jrdp.commons.model.Result;
import org.seven.jrdp.commons.util.JsonUtils;
import org.seven.jrdp.commons.util.ServletUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.servlet.ModelAndView;

@RestController
@RequestMapping("/mgt/sys/param")
public class SysParamController {
	@Autowired
	private SysParamService sysParamService;

	@GetMapping("/{view}")
	public ModelAndView view(@PathVariable String view) {
		return new ModelAndView("mgt/sys/param/param_" + view);
	}

	@PostMapping("/find")
	public List<SysParam> find() {
		List<SysParam> list = sysParamService.find();
		return list;
	}

	@RequestMapping("/save")
	public Result save(String json, HttpServletRequest request) {
		List<SysParam> list = JsonUtils.getList(json, SysParam.class);
		sysParamService.save(list);
		ServletContext servletContext = ServletUtils.getServletContext();
		Map<String, String> paramMap = sysParamService.getParamMap();
		ParamConfig.init(paramMap);
		servletContext.setAttribute(GlobalContants.APPLICATION_PARAM, paramMap);
		return Result.SUCCESS;
	}
}